﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using System.Collections.ObjectModel;
using CookComputing.XmlRpc;
using System.Reflection;

namespace HomeWP
{
    public partial class Devices : PhoneApplicationPage
    {
        private MainPage.HMDataContext hMDB;
        private ObservableCollection<Device> _hMDevices;
        public List<Device> lSwitch;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        public ObservableCollection<Device> HMDevices
        {
            get
            {
                return _hMDevices;
            }
            set
            {
                if (_hMDevices != value)
                {
                    _hMDevices = value;
                    NotifyPropertyChanged("HMDevices");
                }
            }
        }

        public Devices()
        {
            InitializeComponent();

            // Create the database if it does not exist.
            using (MainPage.HMDataContext db = new MainPage.HMDataContext(MainPage.HMDataContext.DBConnectionString))
            {
                if (db.DatabaseExists() == false)
                {
                    //Create the database
                    db.CreateDatabase();
                }
            }

            // Connect to the database and instantiate data context.
            hMDB = new MainPage.HMDataContext(MainPage.HMDataContext.DBConnectionString);

            // Data context and observable collection are children of the main page.
            this.DataContext = this;
        }

        public class Group<T> : ObservableCollection<T>
        {
            public Group(string name, IEnumerable<T> items)
            { this.Key = name; foreach (T item in items) { this.Add(item); } }
            public override bool Equals(object obj)
            { Group<T> that = obj as Group<T>; return (that != null) && (this.Key.Equals(that.Key)); }
            public string Key
            {
                get;
                set;
            
            }
        }

        private IList<Group<Device>> DeviceList;
 
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                // Define the query to gather all of the to-do items.
                var HMItemsInDB = from Device todo in hMDB.HMDevices select todo;

                // Execute the query and place the results into a collection.
                HMDevices = new ObservableCollection<Device>(from Device oItem in hMDB.HMDevices select oItem);
                //HMDeviceNames = new ObservableCollection<DeviceName>(from DeviceName oItem in hMDB.HMDeviceName select oItem);

                //lSwitch = HMDevices.Where(t => t.Type == "SWITCH").ToList();
                List<Device> lDevices = HMDevices.Where(t => !string.IsNullOrEmpty(t.DisplayName)).ToList();

                var DevByType = from dev in lDevices group dev by dev.Type into c orderby c.Key select new Group<Device>(c.Key, c);

                //longListSelector1.ItemsSource = HMDevices.Select(t => t.Type;
                this.DeviceList = DevByType.ToObservableCollection();
                longListSelector1.ItemsSource = DeviceList;

                // Call the base method.
                base.OnNavigatedTo(e);
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
    }

    public static class EnumerableExtensions
    {
        public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> collection)
        {
            ObservableCollection<T> observableCollection = new ObservableCollection<T>();
            foreach (T item in collection)
            {
                observableCollection.Add(item);
            } return observableCollection;
        }
    }
}